﻿using System.Web.Mvc;
using Castle.Windsor;

namespace CMS.Web
{
    /// <summary>
    /// IOC Provider
    /// </summary>
    public static class Provider
    {
        private static readonly IWindsorContainer WindsorContainer;
        private static readonly DefaultControllerFactory DefControllerFactory;

        static Provider()
        {
            WindsorContainer = new WindsorContainer();

            DefControllerFactory = new WindsorControllerFactory(WindsorContainer.Kernel);
        }

        public static IWindsorContainer Container
        {
            get { return WindsorContainer; }
        }

        public static DefaultControllerFactory ControllerFactory
        {
            get { return DefControllerFactory; }
        }

        /// <summary>
        /// Resolve component from container
        /// </summary>
        /// <typeparam name="T">interface of component</typeparam>
        /// <returns>implement of component</returns>
        public static T Resolve<T>()
        {
            return WindsorContainer.Resolve<T>();
        }

        /// <summary>
        /// Resolve component from container
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <typeparam name="T">
        /// interface of component
        /// </typeparam>
        /// <returns>
        /// implement of component
        /// </returns>
        public static T Resolve<T>(string key)
        {
            return WindsorContainer.Resolve<T>(key);
        }
    }
}